﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Resources;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.UI;
using Portal.Core;
using Portal.Core.Helper;
using Portal.Core.Utils;
using Portal.Core.Web.UI.Controls;

public class Common
{
    private static string PARAM_PAGING = "page";

    public static int GetCurrentPageIndex()
    {
        return GetCurrentPageIndex(HttpContext.Current.Request, null);
    }

    public static int GetCurrentPageIndex(HttpRequest request, string keyPaging)
    {
        if (string.IsNullOrEmpty(keyPaging))
            keyPaging = PARAM_PAGING;
        else
        {
            PARAM_PAGING = keyPaging;
        }
        int iCurrPage = 1;
        if (request.QueryString[keyPaging] != null && MyCheck.IsInt(request.QueryString[keyPaging]))
        {
            iCurrPage = Convert.ToInt32(request.QueryString[keyPaging]);
        }
        iCurrPage--;
        return iCurrPage;
    }

    public static MyPaging GetControlPaging(int totalcount, int pagesize, string anchor = null)
    {
        var mypaging = new MyPaging
                                {
                                    CssClass = "paging",
                                    MyTotalCount = totalcount,
                                    MyPageSize = pagesize,
                                    MyParamPaging = PARAM_PAGING,
                                    NextText = "Sau",
                                    PreviousText = "Trước",
                                    NoResultText = "Không có kết quả",
                                    AnchorName = anchor,
                                    MyFormatLeftShow = ""
                                };

        return mypaging;
    }
    public static void AddPagingTool(Control[] objs, int totalrecord, int pagesize, string parampaging = null)
    {
        if (objs.Length <= 0) return;
        foreach (Control t in objs)
        {
            var mypaging = GetControlPaging(totalrecord, pagesize);
            t.Controls.Add(mypaging);
        }
    }

    public static void AddPagingToolAjax(Control[] objs, int totalrecord, int pagesize, string ajaxHolder)
    {
        if (objs.Length <= 0) return;
        var mypaging = GetControlPaging(totalrecord, pagesize);
        mypaging.AjaxActive = true;
        mypaging.AjaxHolder = ajaxHolder;
        var html = mypaging.HtmlContent;
        foreach (Control t in objs)
        {
            t.Controls.Add(new LiteralControl(html));
        }
    }
    public static void PagingWithouTotalCount(Control[] objs, int pageindex, int currentCounter, int pagesize = Portal.Data.Entity.BaseObject.PageSizeDefault)
    {
        if (objs.Length <= 0) return;
        int totalrecord = (pageindex + 4) * pagesize;   //auto create more page
        if (currentCounter < pagesize)
        {
            if (pageindex <= 0)
                return;
            totalrecord = (pageindex + 1) * pagesize;
        }

        foreach (Control t in objs)
        {
            var mypaging = GetControlPaging(totalrecord, pagesize);
            t.Controls.Add(mypaging);
        }
    }
    public static string ExtractNumber(string input)
    {
        string[] numbers = Regex.Split(input, @"\D+");
        string sReturn = "";
        foreach (string value in numbers)
        {
            if (!string.IsNullOrEmpty(value))
                sReturn += value;
        }
        return sReturn.Length > 0 ? sReturn : "0";
    }
    public static string GetRealPath(string imgPath, object width = null)
    {
        return ImageHelper.Instance.GetRealPath(imgPath, width);
    }
    #region Nested type: Nationlities

    public class Nationlities
    {
        public Nationlities(string name)
        {
            Name = name;
        }

        private string _name;
        public string Name { get { return _name; } set { _name = value; } }

        public static List<Nationlities> ListAll()
        {
            List<Nationlities> list = new List<Nationlities>();
            string absolutePath = CoreContext.physicalStaticFile + "Themes/Text/nationlity.txt";
            using (StreamReader sr = new StreamReader(absolutePath))
            {
                string input;
                while ((input = sr.ReadLine()) != null)
                {
                    Nationlities item = new Nationlities(input);
                    list.Add(item);
                }
            }

            return list;
        }
    }

    #endregion

    public static string DateTimeDisplay(DateTime date)
    {
        if (date.Date == DateTime.Today)
            return string.Format("<span class='red'>{0}</span>", date.ToString(Portal.Core.Const.DisplayDateSort));
        return date.ToString(Portal.Core.Const.DisplayDateSort);
    }

    public static string GetString(string fileNameOnly, string fieldName)
    {
        var rm = new ResourceManager("Resources." + fileNameOnly, System.Reflection.Assembly.Load("App_GlobalResources"));
        return rm.GetString(fieldName);
    }

    public static string GetViewId()
    {
        var hash = HttpContext.Current.Request["id"];
        if (!string.IsNullOrEmpty(hash))
        {
            var split = hash.Split('-');
            var viewId = split[split.Length - 1];
            if (MyCheck.IsDouble(viewId))
                return viewId;
        }
        return "0";
    }
    public static string GetCover(object image, int width, object title)
    {
        string path = image != null ? image.ToString() : "";
        return Portal.Data.Entity.Book.RenderImage(path, width, title.ToString(), false);
    }
}